#include "OCMain/FileUtil.h"
#include "OCMain/Environment.h"
#include "OCMain/Log.h"
#include "OCServer/AliasManager.h"
#include "OCServer/ServerUtil.h"
#include "MyCap.h"

using namespace OCPP;

template <typename T>
inline std::vector<T> getYmlVec(const YAML::Node& node)
{
	std::vector<T> vec;
	if (node.Type() == YAML::NodeType::Sequence)
	{
		try {
			for (const auto& c : node)
			{
				vec.push_back(c.as<T>());
			}
		}
		catch (...) {}
	}

	return vec;
}

MyCap* gMyCap = NULL;
void init()
{
	gMyCap = new MyCap();
	String yamlFileName = Singleton(Environment).getAppPath() + "config/OCPP_ControllerCAPService.yml";
	try {
		YAML::Node node = YAML::LoadFile(yamlFileName);
		for (const auto& child : node)
		{
			String name = child.first.as<String>();
#if WIN32
			if (name == "win32") 
#else
			if (name == "linux")
#endif
			{
				gMyCap->_dataPath = U2A(getYmlValue<String>(child.second, "dataPath", ""));
				gMyCap->_outputPath = U2A(getYmlValue<String>(child.second, "outputPath", ""));
				gMyCap->fakeDate = U2A(getYmlValue<String>(child.second, "fakeDate", ""));
				gMyCap->_threadNum = getYmlValue<int>(child.second, "threadNum", 1);
				gMyCap->_expiredTime = getYmlValue<int>(child.second, "expiredTime", 10);
				gMyCap->interval = getYmlValue<int>(child.second, "interval", 6);
				
				LOG_INFO << "dataPath:" << gMyCap->_dataPath << "\n";
				LOG_INFO << "outputPath:" << gMyCap->_outputPath << "\n";
				break;
			}
		}
		gMyCap->startTimer();
	}
	catch (const YAML::Exception& e) {
		LOG_ERROR << e.msg << "\n";
	}
}

void release()
{
	CORE_SAFE_DELETE(gMyCap);
}